package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.activity.MoveDeviceInfoActivity;
import com.audi.dto.DeviceItem;

public class MoveDeviceCardAdapter extends BaseAdapter{
	private Activity mActivity;
	private ArrayList<DeviceItem> mMoveDeviceCardList;
	private CheckBox mCheckSelected;
	private ArrayList<DeviceItem> mListSelectDevice=new ArrayList<DeviceItem>();

	public MoveDeviceCardAdapter(Activity activity,
			ArrayList<DeviceItem> mMoveDeviceCardList){
		mActivity=activity;
		this.mMoveDeviceCardList=mMoveDeviceCardList;

	}

	@Override
	public int getCount(){
		// TODO Auto-generated method stub
		return mMoveDeviceCardList.size();
	}

	@Override
	public Object getItem(int position){
		// TODO Auto-generated method stub
		return mMoveDeviceCardList.get(position);
	}

	@Override
	public long getItemId(int position){
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position,View convertView,
			ViewGroup viewgroup){
		// TODO Auto-generated method stub
		DeviceItem item=mMoveDeviceCardList.get(position);
		ViewHolder holder=null;
		if (convertView==null){
			holder=new ViewHolder();
			convertView=LayoutInflater.from(mActivity).inflate(
					R.layout.row_room_info_device,null);
			holder.imageDevice=(ImageView) convertView
					.findViewById(R.id.image_device);
			holder.nameDevice=(TextView) convertView
					.findViewById(R.id.name_device);
			holder.nameRoom=(TextView) convertView
					.findViewById(R.id.name_room);
			holder.check=(CheckBox) convertView
					.findViewById(R.id.checkbox_device);
			convertView.setTag(holder);

		}else{
			holder=(ViewHolder) convertView.getTag();
		}

		// set data for List

		if (TextUtils.isEmpty(item.getDeviceIcon().trim())){
			holder.imageDevice.setImageResource(R.drawable.icon_device);
		}else{
			BitmapDrawable bitmapDrawable=new BitmapDrawable(
					mActivity.getResources(),item.getDeviceIcon());
			holder.imageDevice.setImageDrawable(bitmapDrawable);
		}

		holder.nameDevice.setText((CharSequence) item.getDeviceName());
		holder.nameRoom.setText((CharSequence) item.getDevicePlace());

		holder.check.setChecked(false);

		holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton arg0,boolean checked){
				if (checked){
					mListSelectDevice.add(mMoveDeviceCardList.get(position));
					new MoveDeviceInfoActivity().mTitleMoveDevice
							.setText("you have selected "
									+mListSelectDevice.size()+" devices");
				} else{
					mListSelectDevice.remove(mMoveDeviceCardList.get(position));
					new MoveDeviceInfoActivity().mTitleMoveDevice
							.setText("you have selected "
									+mListSelectDevice.size()+" devices");
				}

			}
		});

		return convertView;
	}

	public ArrayList<DeviceItem> getListDevice(){
		return mListSelectDevice;
	}

	static class ViewHolder{
		ImageView imageDevice;
		TextView nameDevice;
		TextView nameRoom;
		CheckBox check;
	}
}
